package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.inossem.utils.base.CameraUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingRemoveFileBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingSubmitBean;
import com.inossem.warehousestorage_inossem.manage.ImageManage;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wsy on 2017/11/28 15:21
 * <p>
 * 邮箱：445979770@qq.com
 */

public class EmergencyWarehousingUtils {
    //获取紧急出入库信息
    public static EmergencyWarehousingReceiveOverviewBean getEmergencyWarehousingReceiveOverviewBean(Context context) {
        String json = SPUtils.getReceiveEmergencyWarehousingInformation(context);
        Gson gson = new Gson();
        return gson.fromJson(json, EmergencyWarehousingReceiveOverviewBean.class);
    }

    //过滤紧急出入库货品列表信息
    public static List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> getReceiveEmergencyWarehousingList(Context context, String materiel) {
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> receiveEmergencyWarehousingList = new ArrayList<>();
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> allReceiveEmergencyWarehousingList = getEmergencyWarehousingReceiveOverviewBean(context).getBody().getItem_list();
        for (int i = 0; i < allReceiveEmergencyWarehousingList.size(); i++) {
            EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean item = allReceiveEmergencyWarehousingList.get(i);
            if (item.getMat_code().contains(materiel) || item.getMat_name().contains(materiel)) {
                receiveEmergencyWarehousingList.add(item);
                continue;
            }
        }
        return receiveEmergencyWarehousingList;
    }

    //获取该序号的行项目信息
    public static EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean getReceiveMaterielInformationBySerial(Context context, String serialNumber) {
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> list = getEmergencyWarehousingReceiveOverviewBean(context).getBody().getItem_list();
        for (int i = 0; i < list.size(); i++) {
            EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean item = list.get(i);
            if (serialNumber.equals(item.getUrgence_rid())) {
                return item;
            }
        }
        return null;
    }

    //获取紧急出入库该索引的行项目信息
    public static EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean getReceiveMaterielInformationByIndex(Context context, int index) {
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> list = getEmergencyWarehousingReceiveOverviewBean(context).getBody().getItem_list();
        if (list != null & !list.isEmpty() && list.size() >= index) {
            return list.get(index);
        } else {
            return null;
        }
    }

    //获取紧急出入库行项目数量
    public static int getReceiveMaterielListSize(Context context) {
        return getEmergencyWarehousingReceiveOverviewBean(context).getBody().getItem_list().size();
    }

    //通过序号获取验收入库的索引
    public static int getReceiveIndexBySerial(Context context, String serialNumber) {
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.ItemListBean> list = getEmergencyWarehousingReceiveOverviewBean(context).getBody().getItem_list();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUrgence_rid().equals(serialNumber)) {
                return i;
            }
        }
        return -1;
    }

    //添加紧急出入库验收人,只能有一个经办人
    public static void addReceiveOperator(Context context, GetOperatorBean.OperatorBean receiveItem) {
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<GetOperatorBean.OperatorBean> receiverList = bean.getBody().getUser_list();
        receiverList.clear();
        receiverList.add(0, receiveItem);
        bean.getBody().setUser_list(receiverList);
        Gson gson = new Gson();
        String json = gson.toJson(bean, EmergencyWarehousingReceiveOverviewBean.class);
        SPUtils.saveReceiveEmergencyWarehousingInformation(context, json);
    }

    //删除验收入库验收人
    public static void deleteReceiveOperator(Context context, String id) {
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<GetOperatorBean.OperatorBean> operatorList = bean.getBody().getUser_list();
        for (int i = 0; i < operatorList.size(); i++) {
            GetOperatorBean.OperatorBean operatorItem = operatorList.get(i);
            if (operatorItem.getUser_id().equals(id)) {
                operatorList.remove(operatorItem);
                break;
            }
        }
        bean.getBody().setUser_list(operatorList);
        Gson gson = new Gson();
        String json = gson.toJson(bean, EmergencyWarehousingReceiveOverviewBean.class);
        SPUtils.saveReceiveEmergencyWarehousingInformation(context, json);
    }

    //拼接紧急出入库提交bean
    public static EmergencyWarehousingSubmitBean getEmergencyWarehousingSubmitBean (Context context ,String receiptNumber) {
        EmergencyWarehousingSubmitBean submitBean = new EmergencyWarehousingSubmitBean();
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<GetOperatorBean.OperatorBean> operatorList = bean.getBody().getUser_list();
        submitBean.setUrgence_id(bean.getBody().getUrgence_id());
        submitBean.setReceipt_type(bean.getBody().getReceipt_type());
        submitBean.setStatus(bean.getBody().getStatus_code());
        List<EmergencyWarehousingSubmitBean.UserListBean> operatorListBeanList = new ArrayList<>();
        for (int i = 0; i < bean.getBody().getUser_list().size(); i++) {
            EmergencyWarehousingSubmitBean.UserListBean operatorListBean = new EmergencyWarehousingSubmitBean.UserListBean();
            operatorListBean.setUser_id(operatorList.get(i).getUser_id());
            operatorListBean.setRole_id(operatorList.get(i).getRole_id());
            operatorListBeanList.add(operatorListBean);
        }
        submitBean.setUser_list(operatorListBeanList);
        List<EmergencyWarehousingSubmitBean.FileListBean> uuidListBeanList = new ArrayList<>();
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> fileListBeanList = bean.getBody().getFile_list();
        if (!fileListBeanList.isEmpty()) {
            for (int i = 0; i < fileListBeanList.size(); i++) {
                EmergencyWarehousingSubmitBean.FileListBean uuidListBean = new EmergencyWarehousingSubmitBean.FileListBean();
                uuidListBean.setFile_id(fileListBeanList.get(i).getFile_id());
                uuidListBean.setFile_name(fileListBeanList.get(i).getFile_name());
                uuidListBean.setFile_size(fileListBeanList.get(i).getSize());
                uuidListBeanList.add(uuidListBean);
            }
        }
        submitBean.setFile_list(uuidListBeanList);
        return submitBean;
    }
    //把照相机返回的图片保存到本地file里
    public static void saveFileToSP (Context context , EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean localFileBean) {
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> fileListBean = bean.getBody().getFile_list();
        fileListBean.add(localFileBean);
        Gson gson = new Gson();
        String json = gson.toJson(bean, EmergencyWarehousingReceiveOverviewBean.class);
        SPUtils.saveReceiveEmergencyWarehousingInformation(context, json);
    }
    //删除照片
    public static void deleteFile (Context context , String uuid) {
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> fileListBean = bean.getBody().getFile_list();
        for (int i = 0; i < fileListBean.size(); i++) {
            if (fileListBean.get(i).getFile_id().equals(uuid)) {
                fileListBean.remove(i);
            }
        }
        Gson gson = new Gson();
        String json = gson.toJson(bean, EmergencyWarehousingReceiveOverviewBean.class);
        SPUtils.saveReceiveEmergencyWarehousingInformation(context, json);
    }
    //拼接删除文件request的bean
    public static EmergencyWarehousingRemoveFileBean getDeleteRequestBean (String receiptNumber , String emergencyTypeCode , String uuid) {
        EmergencyWarehousingRemoveFileBean bean = new EmergencyWarehousingRemoveFileBean();
        bean.setUrgence_id(receiptNumber);
        bean.setReceipt_type(emergencyTypeCode);
        bean.setFile_id(uuid);
        return bean;
    }
    //判断有没有经办人
    public static boolean haveAgentPerson (Context context) {
        EmergencyWarehousingReceiveOverviewBean bean = getEmergencyWarehousingReceiveOverviewBean(context);
        List<GetOperatorBean.OperatorBean> operatorListBeanList = bean.getBody().getUser_list();
        if (operatorListBeanList.isEmpty()) {
            return false;
        }
        return true;
    }
    public static void createEditText(Context context, ViewGroup viewGroup, List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> list,List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> mLocalFileBean) {
        LayoutInflater inflater = LayoutInflater.from(context);
//        PercentLinearLayout.LayoutParams layoutParams = new PercentLinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        layoutParams.gravity = Gravity.RIGHT;
        for (int i = 0; i < list.size(); i++) {
            View view = inflater.inflate(R.layout.view_emergency_warehousing_image, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            ImageView delete = (ImageView) view.findViewById(R.id.delete);
            inLocal(context , imageView , i , list , mLocalFileBean);
            view.setId(i);
            viewGroup.addView(view);
        }
        if (list.size() != 3) {
            View view = inflater.inflate(R.layout.view_emergency_warehousing_image, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            imageView.setBackgroundResource(R.drawable.add_picture);
//            view.setLayoutParams(layoutParams);
            viewGroup.addView(view);
        }
    }
    private static boolean inLocal (Context mContext ,ImageView imageView , int where ,List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> fileBeanItemList ,List<EmergencyWarehousingReceiveOverviewBean.DataBean.FileListBean> mLocalFileBean) {
        int inLocalPosition;
        if (mLocalFileBean.size() != 0) {
            int a = 1000;
            for (int i = 0; i < mLocalFileBean.size(); i++) {
                if (fileBeanItemList.get(where).getFile_id().equals(mLocalFileBean.get(i).getFile_id())) {
                    a = i;
                }
            }
            if (a == 1000) {
                ImageManage.requestImage(mContext, imageView, fileBeanItemList.get(where).getFile_url());
            }else {
                inLocalPosition = a;
                imageView.setImageBitmap(CameraUtils.uriToBitmap(mLocalFileBean.get(a).getBitmapPath()));
                return true;
            }
        }
        ImageManage.requestImage(mContext, imageView, fileBeanItemList.get(where).getFile_url());

        return false;

    }
}
